/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Taxi.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author U24022
 */
public class Database {

    /* Use one JDBC connection througout the program. */
    private static Connection connection;

    public static void connectToDB(String dburl, String uname, String passwd) throws ClassNotFoundException {
        try {
       // Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(dburl, uname, passwd);
        } catch (SQLException connectException) {
            System.out.println(connectException.getMessage());
            System.out.println(connectException.getSQLState());
            System.out.println(connectException.getErrorCode());
            System.exit(1);
        }
    }

    public static Connection getConnection() {
        return connection;
    }


    public void close(ResultSet rs){

        if(rs !=null){
            try{
               rs.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }

        }
    }

     public void close(java.sql.Statement stmt){

        if(stmt !=null){
            try{
               stmt.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }

        }
    }

  public void destroy(){

    if(connection !=null){

         try{
               connection.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }


    }
  }


}
